package com.sailors.common.task.provider.infra.design.common.util;

import lombok.extern.slf4j.Slf4j;

import java.util.function.Supplier;

/**
 * Try catch util
 *
 * @description: try catch 工具
 * @author: cyj
 * @date 2022 -10-28 14:31:18
 */
@Slf4j
public class TryCatchUtil {

    @FunctionalInterface
    public interface Executor {
        void execute();
    }

    /**
     * Try catch t
     *
     * @param <T>     parameter
     * @param execute execute
     * @return the t
     */
    public static <T> T tryC(Supplier<T> execute) {
        try {
            return execute.get();
        } catch (Exception e) {
            log.warn("try catch execute warn:{}", e.getMessage());
            return null;
        }
    }

    /**
     * Try catch t
     *
     * @param
     * @param executor executor
     * @return the t
     */
    public static void tryC(Executor executor) {
        try {
            executor.execute();
        } catch (Exception e) {
            log.warn("try catch execute error:{}", e.getMessage());
        }
    }

}
